from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.users.models import User
from apps.userinfo.serializers import UserSerializer, UserPasswordSerializer


class UserInfoView(APIView):
    '''个人中心'''
    def get(self,request):
        user = request.user
        spec = User.objects.get(username = user)
        serializer = UserSerializer(spec)

        return Response(serializer.data)

    def put(self,request):
        user = request.user
        spec = User.objects.get(username=user)
        data = request.data
        serializer = UserSerializer(instance=spec,data=data)
        if serializer.is_valid():
            serializer.save()
        else:
            return Response({
                'message': '修改失败',
                'success': False
            })
        return Response(serializer.data)

class UserPasswordView(APIView):
    '''修改密码'''
    def put(self,request):
        user = request.user
        user = User.objects.get(username=user)
        serializer = UserPasswordSerializer(instance=user,data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({
                        'message': '修改成功',
                        'success': True
                })
        else:
            return Response({
                'message': '修改失败',
                'success': True
            })

class UserLikeView(APIView):
    '''关注用户'''
    def post(self,request,id):
        user = request.user
        user = User.objects.get(username=user)
        fans = User.objects.get(pk=id)
        user.fans.add(fans)

        return Response({
            'message': '关注成功',
            'success': True
        })

    def delete(self,request,id):
        user = request.user
        user = User.objects.get(username=user)
        fans = User.objects.get(pk=id)
        user.fans.remove(fans)

        return Response({
            'message': '取消关注成功',
            'success': True
        })